package com.imist.mybatisdemo2.servlet;

import com.imist.mybatisdemo2.dao.UserDao;
import com.imist.mybatisdemo2.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
@WebServlet("/adduser")
public class AddUserServlet extends HttpServlet {
    private UserDao userDao = new UserDao();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取要添加的用户数据
        String username = req.getParameter("username");
        String userpass = req.getParameter("userpass");
        String nickname = req.getParameter("nickname");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        //根据用户数据创建用户对象
        User user = new User(username,userpass,nickname,Integer.parseInt(age),gender,phone,email,new Date(),new Date(),new Date(),0);
        //将用户对象添加到数据库中
        user = userDao.addUser(user);
        //查看新增的用户数据
        resp.sendRedirect("/detail?id="+user.getId());
    }
}
